initUsers();
function initUsers() {
  $.ajax({
    url: '/users.do',
    method: 'GET',
    success: function (result) {
      if (result && result.length > 0) {
        for (var i = 0; i < result.length; i++) {
          var val = result[i];
          $(`<tr>
                <td>${val.id}</td>
                <td>${val.username}</td>
                <td>${val.phone}</td>
                <td>${val.state == 1 ? '正常' : '冻结'}</td>
                <td>
                  <button class="am-btn am-btn-success resetPass" myId="${val.id}">重置密码</button>
                  ${val.state == 1 ? `<button class="am-btn am-btn-danger state_change" myId="${val.id}">冻结</button>` : `<button class="am-btn am-btn-primary state_change" myId="${val.id}">解冻</button>`}
                </td>
            </tr>`).appendTo('tbody');
        }
      }
    }
  })
}


$(document).on('click', ".resetPass", function () {
  let id = $(this).attr('myId');
  swal({
    title: '请输入新密码',
    input: 'password',
    showCancelButton: true,
    inputValidator: function (value) {
      return new Promise(function (resolve, reject) {
        if (value) {
          resolve()
        } else {
          reject('密码不能为空')
        }
      })
    }
  }).then(function (result) {
    if(!result){return;}
    //Ajax
    $.ajax({
      url:'/resetPass.do',
      method:'POST',
      data:`id=${id}&password=${result}`,
      success:function(result){
        if(result==='success'){
          swal('修改成功','密码重置成功','success');
        }else{
          swal('修改失败','密码重置失败','error');
        }
      }
    })

  })

})


$(document).on('click', '.state_change', function () {
  let txt = $(this).html();
  let id = $(this).attr('myId');
  let state = txt=='冻结'?0:1;
  swal({ 
    title: '确定要'+txt+'吗？', 
    type: 'warning',
    showCancelButton: true, 
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: '确定', 
  }).then(function(result){
if(!result){
  return;
}
    //Ajax
    $.ajax({
      url:'/change_state.do',
      method:'POST',
      data:`id=${id}&state=${state}`,
      success:function(result){
        if(result=='success'){
          location.reload();
        }else{
          swal(txt+'失败','后台繁忙，请稍后再试','error');
        }
      }
    })
  })
})

$('.add_user').click(function () {
  swal({
    title: '添加用户',
    html:`<input id="username" class="swal2-input"> <input id="password" class="swal2-input"><input id="phone" class="swal2-input">`,
    preConfirm: function () {
      return new Promise(function (resolve) {
        resolve([
          $('#username').val(),
          $('#password').val(),
          $('#phone').val(),
        ])
      })
    },
    onOpen: function () {
      $('#username').focus()
    }
  }).then(function (result) {
      $.ajax({
          url:'/addUser.do',
          method:'post',
          data:`username= ${$('#username').val()}&password=${$('#password').val()}&phone=${$('#phone').val()}`,
          success:function(result){
            console.log(result);
            if(result=='success'){
              location.reload();
            }else{
              swal('添加失败','请完善信息','error');
            }
          }
      });
  
  }).catch(swal.noop)
  });